package com.lh0811.forge.dependency.basic_code.encrypt;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RsaUtil {

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    // 生成密钥对
    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        keyPairGen.initialize(1024);
        return keyPairGen.generateKeyPair();
    }

    // 公钥加密
    public static String encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    // 私钥解密
    public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decryptedBytes);
    }

    // 从字符串加载公钥
    public static PublicKey loadPublicKey(String publicKeyStr) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(publicKeyStr);
        X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(spec);
    }

    // 从字符串加载私钥
    public static PrivateKey loadPrivateKey(String privateKeyStr) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(privateKeyStr);
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(spec);
    }

    public static void main(String[] args) throws Exception {
        String encrypt = RsaUtil.encrypt("123123", loadPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDe5nNLq6sQxB4lZaxURuebnkaNk3vTg4V7ji3vOHK58u/L2ql83jsJ3M66fgjQ+t1iMr65QoHJ3Ngiizxf1pwSRtkFat/89QPPdfnk7mcxGMMUL1DDadOAh4FsgAPtgUCVpzNFYQJSsWm/GLV+dkjpWKtGFuxggGKOMG8h276BcQIDAQAB"));
        System.out.printf("encrypt:" + encrypt);
        String decrypt = RsaUtil.decrypt(encrypt, loadPrivateKey("MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAN7mc0urqxDEHiVlrFRG55ueRo2Te9ODhXuOLe84crny78vaqXzeOwnczrp+CND63WIyvrlCgcnc2CKLPF/WnBJG2QVq3/z1A891+eTuZzEYwxQvUMNp04CHgWyAA+2BQJWnM0VhAlKxab8YtX52SOlYq0YW7GCAYo4wbyHbvoFxAgMBAAECgYAcMuVVJmOe3GC/9GvnwYydeFmOoaDhslrZtbQD9o01DxHdJU2Z2Xd4mPGiUD2HSqVlPew4/bvzsLe7Lwg1S640uUbUNk5RkZ7N3es2hbAToIRIFYmiOBs2VKnYJkqY01jKseLX2MOhrA4Tkjs3C99qgksI3BOr/jifONqUykSJ3QJBAO+Htskt6IUNRqbNBbT9cZiSBB0csvNmwxjNpOUygCRFL2uojJPslkVMVe+YeC1LXVD22mUTFtobNL12v4ILpnMCQQDuOgLLPU60MHmoRJpHgVvveYMftIzng2vEWWivY8+Np+BMegaXj4A43fhrcYajMsRT3HaXP1Vi0x2An7eDbxuLAkEApSKNU7lAuqnicZCbRKIONL9F8sQcY04PZwQvkIv2aAsCnFhBQqIzH2d3P42AHMn+dZkaC+DTOl7TR2OcuA1MHQJBAIqJcdZ/cKWZCyavvoiW0cASCg8GEGsSIjmtRRO6F5lpZycST4MUEJ0BeNYCzylY/JNIQi7yxdUSsLV8LFxi4MECQQCFic66f1W+M6nPJ0skgku87INEnBuL6BL/dYheRemiWKWDd4aGkXqdrTT719NcD9TzywPRQ/id6JXG+XLCEbbA"));
        System.out.printf("decrypt:" + decrypt);
    }
}
